package org.zirco.ui.adapters;

import java.util.List;

import org.zirco.model.RecommendNavigationItem;
import org.zirco.ui.activities.ZircoMain;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;


public class NavigateGridAdapter extends BaseAdapter {

	public static final int MAX_SHOW_GRIDS = 4;
	
	private List<RecommendNavigationItem> recommendAddressList;
	private Context mContext;
	
	public NavigateGridAdapter(Context context, List<RecommendNavigationItem> listData){
		recommendAddressList = listData;
		mContext = context;
	}
	
	@Override
	public int getCount() {
		int count = recommendAddressList.size();
		if(count > MAX_SHOW_GRIDS){
			count = MAX_SHOW_GRIDS;
		}
		return count;
	}

	@Override
	public RecommendNavigationItem getItem(int position) {
		return recommendAddressList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = new TextView(mContext);
		((TextView)convertView).setText(recommendAddressList.get(position).getItemName());
		((TextView)convertView).setTag(position);
		((TextView)convertView).setGravity(Gravity.CENTER);
		((TextView)convertView).setLayoutParams(new GridView.LayoutParams(50,50));
		((TextView)convertView).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int pos = (Integer) v.getTag();
				Intent it = new Intent(mContext, ZircoMain.class);
			    it.setData(Uri.parse(getItem(pos).getItemLinkURL()));
//			    it = Intent.createChooser(it, null);
			    mContext.startActivity(it);
			}
		});
		return convertView;
	}

}
